{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(8).reshape(2, 2, 2)\n",
    "arr2 = np.random.rand(2, 2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3]],\n",
       "\n",
       "       [[4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.28718568, 0.46697359],\n",
       "        [0.56791058, 0.72510447]],\n",
       "\n",
       "       [[0.03667108, 0.90077012],\n",
       "        [0.94011437, 0.95437399]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.28718568, 1.46697359],\n",
       "        [2.56791058, 3.72510447]],\n",
       "\n",
       "       [[4.03667108, 5.90077012],\n",
       "        [6.94011437, 7.95437399]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.        , 0.46697359],\n",
       "        [1.13582116, 2.1753134 ]],\n",
       "\n",
       "       [[0.14668434, 4.50385059],\n",
       "        [5.64068621, 6.68061796]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 * arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  2.87185675, 104.66973588],\n",
       "        [205.67910579, 307.25104467]],\n",
       "\n",
       "       [[400.36671085, 509.00770118],\n",
       "        [609.40114368, 709.54373994]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 * 100 + arr2 * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  0, 100],\n",
       "        [200, 300]],\n",
       "\n",
       "       [[400, 500],\n",
       "        [600, 700]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2,  4],\n",
       "       [ 6,  8, 10],\n",
       "       [12, 14, 16]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def loop_double(arr):\n",
    "    n, m = arr.shape\n",
    "    for i in range(n):\n",
    "        for j in range(m):\n",
    "            arr[i][j] *= 2\n",
    "    return arr\n",
    "loop_double(np.arange(9).reshape(3, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.61 ms ± 203 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit loop_double(np.arange(10000).reshape(100, 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.9 µs ± 307 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit np.arange(10000).reshape(100, 100) * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr3 = np.arange(8*5*3).reshape(8, 5, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7140"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[420, 428, 436],\n",
       "       [444, 452, 460],\n",
       "       [468, 476, 484],\n",
       "       [492, 500, 508],\n",
       "       [516, 524, 532]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(arr3, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 30,  35,  40],\n",
       "       [105, 110, 115],\n",
       "       [180, 185, 190],\n",
       "       [255, 260, 265],\n",
       "       [330, 335, 340],\n",
       "       [405, 410, 415],\n",
       "       [480, 485, 490],\n",
       "       [555, 560, 565]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(arr3, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  3,  12,  21,  30,  39],\n",
       "       [ 48,  57,  66,  75,  84],\n",
       "       [ 93, 102, 111, 120, 129],\n",
       "       [138, 147, 156, 165, 174],\n",
       "       [183, 192, 201, 210, 219],\n",
       "       [228, 237, 246, 255, 264],\n",
       "       [273, 282, 291, 300, 309],\n",
       "       [318, 327, 336, 345, 354]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(arr3, axis=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  3],\n",
       "        [ 12],\n",
       "        [ 21],\n",
       "        [ 30],\n",
       "        [ 39]],\n",
       "\n",
       "       [[ 48],\n",
       "        [ 57],\n",
       "        [ 66],\n",
       "        [ 75],\n",
       "        [ 84]],\n",
       "\n",
       "       [[ 93],\n",
       "        [102],\n",
       "        [111],\n",
       "        [120],\n",
       "        [129]],\n",
       "\n",
       "       [[138],\n",
       "        [147],\n",
       "        [156],\n",
       "        [165],\n",
       "        [174]],\n",
       "\n",
       "       [[183],\n",
       "        [192],\n",
       "        [201],\n",
       "        [210],\n",
       "        [219]],\n",
       "\n",
       "       [[228],\n",
       "        [237],\n",
       "        [246],\n",
       "        [255],\n",
       "        [264]],\n",
       "\n",
       "       [[273],\n",
       "        [282],\n",
       "        [291],\n",
       "        [300],\n",
       "        [309]],\n",
       "\n",
       "       [[318],\n",
       "        [327],\n",
       "        [336],\n",
       "        [345],\n",
       "        [354]]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(arr3, axis=2, keepdims=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
